#include"ThreadPool.hpp"
#include"Route.hpp"
#include"ChatServer.hpp"
#include<iostream>
#include<memory>

void Usage(std::string proc)
{
    std::cout << "Usage: " << proc << "localport" <<std::endl;
}

using task_t = std::function<void()>;

//./udp_server serverport
int main(int argc , char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    EnableConsoleLogStrategy();
    std::cout << "1111111111111"<<std::endl;

    uint16_t port = std::stoi(argv[1]);
    std::cout << "2222222222222222"<<std::endl;


    //1、消息转发功能
    std::unique_ptr<Route> r = std::make_unique<Route>();
    std::cout << "3333333333333333"<<std::endl;


    //2、线程池对象
    auto tp = ThreadPool<task_t>::GetInstance();

    std::cout << "444444444444444"<<std::endl;

    //服务器对象
    std::unique_ptr<ChatServer> usvr = std::make_unique<ChatServer>(port ,
        [&r, &tp](int sockfd , std::string message , InetAddr addr)
    {
        task_t task = std::bind(&Route::RouteMessageToAll , r.get() , sockfd , message , addr);
        tp->Enqueue(task);
    });
    usvr ->Init();
    usvr->Start();
    return 0;
}